#Boa:Frame:Frame1

import os, Queue, thread, time, wx
from Download import Download

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1DOWNLOADBUTTON, wxID_FRAME1NOTEBOOK1, 
 wxID_FRAME1PROFILECOMBOBOX, wxID_FRAME1PROGRESSGUAGE, wxID_FRAME1STATUSBAR1, 
 wxID_FRAME1TEXTCTRL1, wxID_FRAME1TOOLBAR1, 
] = [wx.NewId() for _init_ctrls in range(8)]

[wxID_FRAME1FILEMENUEXIT] = [wx.NewId() for _init_coll_fileMenu_Items in range(1)]

class Frame1(wx.Frame):
    def _init_coll_toolBarSizer_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.profileComboBox, 0, border=0, flag=0)
        parent.AddWindow(self.downloadButton, 0, border=0, flag=0)
        parent.AddWindow(self.progressGuage, 0, border=0, flag=0)

    def _init_coll_menuBar1_Menus(self, parent):
        # generated method, don't edit

        parent.Append(menu=self.fileMenu, title='&File')

    def _init_coll_fileMenu_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='', id=wxID_FRAME1FILEMENUEXIT, kind=wx.ITEM_NORMAL,
              text='E&xit')
        self.Bind(wx.EVT_MENU, self.OnFileMenuExitMenu,
              id=wxID_FRAME1FILEMENUEXIT)

    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=-1, page=self.textCtrl1, select=True,
              text='Download')

    def _init_coll_statusBar1_Fields(self, parent):
        # generated method, don't edit
        parent.SetFieldsCount(2)

        parent.SetStatusText(number=0, text='Fields0')
        parent.SetStatusText(number=1, text='Fields1')

        parent.SetStatusWidths([-1, -1])

    def _init_sizers(self):
        # generated method, don't edit
        self.toolBarSizer = wx.BoxSizer(orient=wx.HORIZONTAL)

        self._init_coll_toolBarSizer_Items(self.toolBarSizer)

    def _init_utils(self):
        # generated method, don't edit
        self.menuBar1 = wx.MenuBar()

        self.fileMenu = wx.Menu(title='')

        self._init_coll_menuBar1_Menus(self.menuBar1)
        self._init_coll_fileMenu_Items(self.fileMenu)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(371, 490), size=wx.Size(400, 300),
              style=wx.DEFAULT_FRAME_STYLE, title='Asylum')
        self._init_utils()
        self.SetClientSize(wx.Size(392, 266))
        self.SetMenuBar(self.menuBar1)

        self.toolBar1 = wx.ToolBar(id=wxID_FRAME1TOOLBAR1, name='toolBar1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(392, 28),
              style=wx.TB_HORIZONTAL | wx.NO_BORDER)
        self.SetToolBar(self.toolBar1)

        self.statusBar1 = wx.StatusBar(id=wxID_FRAME1STATUSBAR1,
              name='statusBar1', parent=self, style=0)
        self.statusBar1.SetStatusText('')
        self._init_coll_statusBar1_Fields(self.statusBar1)
        self.SetStatusBar(self.statusBar1)

        self.notebook1 = wx.Notebook(id=wxID_FRAME1NOTEBOOK1, name='notebook1',
              parent=self, pos=wx.Point(0, 28), size=wx.Size(392, 195),
              style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(384, 169),
              style=wx.TE_MULTILINE, value='')
        self.textCtrl1.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
              'Courier New'))

        self.downloadButton = wx.BitmapButton(bitmap=wx.Bitmap('C:/asylum/060730/File.png',
              wx.BITMAP_TYPE_PNG), id=wxID_FRAME1DOWNLOADBUTTON,
              name='downloadButton', parent=self.toolBar1, pos=wx.Point(130, 0),
              size=wx.Size(24, 24), style=wx.BU_AUTODRAW)
        self.downloadButton.SetToolTipString('Download Button')
        self.downloadButton.Bind(wx.EVT_BUTTON, self.OnDownloadButton,
              id=wxID_FRAME1DOWNLOADBUTTON)

        self.progressGuage = wx.Gauge(id=wxID_FRAME1PROGRESSGUAGE,
              name='progressGuage', parent=self.toolBar1, pos=wx.Point(154, 0),
              range=100, size=wx.Size(230, 24), style=wx.GA_HORIZONTAL)

        self.profileComboBox = wx.ComboBox(choices=[],
              id=wxID_FRAME1PROFILECOMBOBOX, name='profileComboBox',
              parent=self.toolBar1, pos=wx.Point(0, 0), size=wx.Size(130, 21),
              style=0, value='Profiles')
        self.profileComboBox.SetLabel('Profiles')
        self.profileComboBox.Bind(wx.EVT_COMBOBOX,
              self.OnProfileComboBoxCombobox, id=wxID_FRAME1PROFILECOMBOBOX)

        self._init_coll_notebook1_Pages(self.notebook1)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.queue = Queue.Queue()
        self.Bind(wx.EVT_IDLE, self.OnIdle)
        self.initProfile()

    def OnIdle(self, event):
        while not self.queue.empty():
            msg = self.queue.get()
            if msg.find('STATUS|') > -1:
                a = msg.split('|')
                self.SetStatusText(number=0, text=a[1])
            elif msg.find('ERROR|') > -1:
                a = msg.split('|')
                saved = self.statusBar1.GetStatusText(number=0)
                self.SetStatusText(number=0, text=a[1])
                time.sleep(1)
                self.SetStatusText(number=0, text=saved)
            else: self.textCtrl1.write(msg)

    def OnFileMenuExitMenu(self, event):
        self.Close(True)

    def OnDownloadButton(self, event):
        thread.start_new_thread(self.download, (self,))

    def download(self, *args):
        parent = args[0]
        d = Download(parent)

    def initProfile(self):
        self.profile = 'Songbird'
        self.SetStatusText(number=0, text='Select a profile.')
        self.SetStatusText(number=1, text='Profile: %s' % self.profile)
        for file in os.listdir('.'):
            if file.find('.cfg') > -1:
                self.profileComboBox.Append(file)

    def OnProfileComboBoxCombobox(self, event):
        file = event.GetString()
        if file == 'songbird.cfg':
            self.SetStatusText(number=1, text='Profile: Songbird')
        elif file == 'enlightenment.cfg':
            self.SetStatusText(number=1, text='Profile: Enlightenment')
        fr = open(file, 'r')
        s1 = fr.read()
        fr.close()
        a1 = s1.rstrip().split('\n')
        for s2 in a1:
            a2 = s2.split('|')
            if a2[0] == 'sUrltemplate':
                self.sUrltemplate = a2[1]
            elif a2[0] == 'sFiletemplate':
                self.sFiletemplate = a2[1]
        self.SetStatusText(number=0, text='Click the Download button.')














